package com.web.servlet.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.web.servlet.bean.UserInfo;
import com.web.servlet.dao.UserInfoDao;
import com.web.servlet.db.DbParameter;
import com.web.servlet.db.StrategyBaseDao;

public class UserInfoDaoImpl extends StrategyBaseDao implements UserInfoDao {
	public static final Logger log = LoggerFactory.getLogger(UserInfoDaoImpl.class);
	@Override
	public UserInfo addUserInfo(UserInfo userInfo) {
		userInfo.setUser_id(getUserId());
		addUser(userInfo);
		return userInfo;
	}
	public int getUserId(){
		String sqlText = "SELECT COUNT(*) as num from g_user where user_id=?;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		Random random=new Random();//定义随机类intresult=random.nextInt(10);
		int userid = random.nextInt(99999999);
		int num = 1; 
		para.put(1, new DbParameter(Types.INTEGER, userid));
		PreparedStatement pstmt = execQuery(sqlText.toString(), para);
		ResultSet rs = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					num = rs.getInt("num");
				}
			} catch (Exception e) {
				log.error("获取userid 是否使用", e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		if(0 == num){
			return userid;
		}else{
			return getUserId();
		}
	}
	public boolean addUser(UserInfo info) {
		boolean result = false;
		String sqlString = "  Insert Into g_user(user_id,user_name,pass_world,nick_name,gold,diamond,icon,sex,create_time,last_logintime,login_ip,score,signature,imei,channel,user_type)  VALUE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.INTEGER, info.getUser_id()));
		para.put(2, new DbParameter(Types.VARCHAR, info.getUser_name()));
		para.put(3, new DbParameter(Types.VARCHAR, info.getPass_world()));
		para.put(4, new DbParameter(Types.VARCHAR, info.getNick_name()));
		para.put(5, new DbParameter(Types.INTEGER, info.getGold()));
		para.put(6, new DbParameter(Types.INTEGER, info.getDiamond()));
		para.put(7, new DbParameter(Types.VARCHAR, info.getIcon()));
		para.put(8, new DbParameter(Types.TINYINT, info.getSex()));
		para.put(9, new DbParameter(Types.TIMESTAMP, info.getCreate_time()));
		para.put(10, new DbParameter(Types.TIMESTAMP, info.getLast_logintime()));
		para.put(11, new DbParameter(Types.VARCHAR, info.getLogin_ip()));
		para.put(12, new DbParameter(Types.INTEGER, info.getScore()));
		para.put(13, new DbParameter(Types.VARCHAR, info.getSignature()));
		para.put(14, new DbParameter(Types.VARCHAR, info.getImei()));
		para.put(15, new DbParameter(Types.VARCHAR, info.getChannel()));
		para.put(16, new DbParameter(Types.INTEGER, info.getUser_type()));
		result = execNoneQuery(sqlString, para) > -1 ? true : false;
		return result;
	}
	
	
	
	@Override
	public UserInfo getUserInfoByName(String userName, String passworld) {
		String sqlText = "select * from g_user where user_name=? and pass_world=?;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.VARCHAR, userName));
		para.put(2, new DbParameter(Types.VARCHAR, passworld));
		return getUserInfo(sqlText, para);
	}
	@Override
	public UserInfo getUserInfoByName(String userName) {
		String sqlText = "select * from g_user where user_name=? ;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.VARCHAR, userName));
		return getUserInfo(sqlText, para);
	}
	private UserInfo getUserInfo(String sqlText, Map<Integer, DbParameter> para) {
		PreparedStatement pstmt = execQuery(sqlText, para);
		ResultSet rs = null;
		UserInfo info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new UserInfo();
					info.setUser_id(rs.getInt("user_id"));
					info.setUser_name(rs.getString("user_name"));
					info.setPass_world(rs.getString("pass_world"));
					info.setNick_name(rs.getString("nick_name"));
					info.setGold(rs.getInt("gold"));
					info.setDiamond(rs.getInt("diamond"));
					info.setIcon(rs.getString("icon"));
					info.setSex(rs.getInt("sex"));
					info.setCreate_time(rs.getTimestamp("create_time"));
					info.setLast_logintime(rs.getTimestamp("last_logintime"));
					info.setLogin_ip(rs.getString("login_ip"));
					info.setScore(rs.getInt("score"));
					info.setSignature(rs.getString("signature"));
					info.setImei(rs.getString("imei"));
					info.setChannel(rs.getString("channel"));
					info.setUser_type(rs.getInt("user_type"));
					
				}
			} catch (SQLException e) {
				info = null;
				log.error("执行出错" + sqlText, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

}
